﻿using Model;
using Model.Modules;
using Proxy;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using Utility;

namespace ViewModel
{
    public class VM_PageImageView : BaseViewModel
    {
        #region 变量
        public static surfacedetectEntities surfaceEntities;


        public int CurrentImageType
        {
            get { return currentImageType; }
            set
            {
                currentImageType = value;
                OnPropertyChanged();
                updateImage();
            }
        }
        private int currentImageType;

        public ObservableCollection<SurfacePartInfo> SurfacePartInfoList { get { return surfacePartInfoList; } set { surfacePartInfoList = value; OnPropertyChanged(); } }
        private ObservableCollection<SurfacePartInfo> surfacePartInfoList;

        public ObservableCollection<Imagedefectinfo> Imagelist1 { get { return imagelist1; } set { imagelist1 = value; OnPropertyChanged(); } }
        private ObservableCollection<Imagedefectinfo> imagelist1;

        public ObservableCollection<Imagedefectinfo> Imagelist2 { get { return imagelist2; } set { imagelist2 = value; OnPropertyChanged(); } }
        private ObservableCollection<Imagedefectinfo> imagelist2;

        public ObservableCollection<Imagedefectinfo> Imagelist3 { get { return imagelist3; } set { imagelist3 = value; OnPropertyChanged(); } }
        private ObservableCollection<Imagedefectinfo> imagelist3;

        public ObservableCollection<Imagedefectinfo> Imagelist4 { get { return imagelist4; } set { imagelist4 = value; OnPropertyChanged(); } }
        private ObservableCollection<Imagedefectinfo> imagelist4;

        public ObservableCollection<Imagedefectinfo> Imagelist5 { get { return imagelist5; } set { imagelist5 = value; OnPropertyChanged(); } }
        private ObservableCollection<Imagedefectinfo> imagelist5;

        public ObservableCollection<Imagedefectinfo> Imagelist6 { get { return imagelist6; } set { imagelist6 = value; OnPropertyChanged(); } }
        private ObservableCollection<Imagedefectinfo> imagelist6;

        public ObservableCollection<Imagedefectinfo> Imagelist7 { get { return imagelist7; } set { imagelist7 = value; OnPropertyChanged(); } }
        private ObservableCollection<Imagedefectinfo> imagelist7;

        public ObservableCollection<Imagedefectinfo> Imagelist8 { get { return imagelist8; } set { imagelist8 = value; OnPropertyChanged(); } }
        private ObservableCollection<Imagedefectinfo> imagelist8;

        public List<ObservableCollection<Imagedefectinfo>> ImgList_Collection { set; get; }

        public ObservableCollection<Imagedefectinfo> Defectlist { get { return defectlist; } set { defectlist = value; OnPropertyChanged(); } }
        private ObservableCollection<Imagedefectinfo> defectlist;

        public Visibility QueryCtlVisiable { get { return queryCtlVisiable; } set { queryCtlVisiable = value; OnPropertyChanged(); } }
        private Visibility queryCtlVisiable;

        public Imagedefectinfo CurrentImg1 { get { return currentImg1; } set { currentImg1 = value; OnPropertyChanged(); } }
        private Imagedefectinfo currentImg1;
        public Imagedefectinfo CurrentImg2
        {
            get { return currentImg2; }
            set
            {
                currentImg2 = value;
                OnPropertyChanged();
            }
        }
        private Imagedefectinfo currentImg2;
        public Imagedefectinfo CurrentImg3
        {
            get { return currentImg3; }
            set
            {
                currentImg3 = value;

                OnPropertyChanged();
            }
        }
        private Imagedefectinfo currentImg3;
        public Imagedefectinfo CurrentImg4
        {
            get { return currentImg4; }
            set
            {
                currentImg4 = value;

                OnPropertyChanged();
            }
        }
        private Imagedefectinfo currentImg4;

        public Imagedefectinfo CurrentImg5
        {
            get { return currentImg5; }
            set
            {
                currentImg5 = value;

                OnPropertyChanged();
            }
        }
        private Imagedefectinfo currentImg5;
        public Imagedefectinfo CurrentImg6
        {
            get { return currentImg6; }
            set
            {
                currentImg6 = value;

                OnPropertyChanged();
            }
        }
        private Imagedefectinfo currentImg6;
        public Imagedefectinfo CurrentImg7
        {
            get { return currentImg7; }
            set
            {
                currentImg7 = value;

                OnPropertyChanged();
            }
        }
        private Imagedefectinfo currentImg7;
        public Imagedefectinfo CurrentImg8
        {
            get { return currentImg8; }
            set
            {
                currentImg8 = value;

                OnPropertyChanged();
            }
        }
        private Imagedefectinfo currentImg8;

        private List<Imagedefectinfo> CurrentImgList { set; get; }

        public ObservableCollection<batch> Batchlist { get { return batchlist; } set { batchlist = value; OnPropertyChanged(); } }
        private ObservableCollection<batch> batchlist;
        public batch CurrentBatch
        {
            get { return currentBatch; }
            set
            {
                currentBatch = value;
                OnPropertyChanged();
                if (CurrentBatch != null && !StaticData.IsRefreshing)
                {
                    if (StaticData.DataBase == DataFrom.Test) StaticData.TestEntitity = new TestEntitity(CurrentBatch.main_id);
                    _ = QueryImageInfo();
                }

            }
        }
        private batch currentBatch;
        public DateTime Start { get { return start; } set { start = value; OnPropertyChanged(); } }
        private DateTime start;
        public DateTime End { get { return end; } set { end = value; OnPropertyChanged(); } }
        private DateTime end;

        public bool UseStart { get { return useStart; } set { useStart = value; OnPropertyChanged(); } }
        private bool useStart;
        public bool UseEnd { get { return useEnd; } set { useEnd = value; OnPropertyChanged(); } }
        private bool useEnd;

        public bool IsLoaded
        {
            get { return isLoaded; }
            set
            {
                isLoaded = value;
                if (isLoaded)
                    QueryCtlVisiable = StaticData.IsRefreshing ? Visibility.Hidden : Visibility.Visible;
                OnPropertyChanged();
            }
        }
        private bool isLoaded;

        public bool IsQueryCompleted { get; set; }


        public int CurrentImageIndex
        {
            get { return currentImageIndex; }
            set
            {
                currentImageIndex = value;
                OnPropertyChanged();
            }
        }
        private int currentImageIndex;


        public int CurrentCamera { get { return currentCamera; } set { currentCamera = value; OnPropertyChanged(); } }
        private int currentCamera;

        public ObservableCollection<int> Cameras { get { return cameras; } set { cameras = value; OnPropertyChanged(); } }
        private ObservableCollection<int> cameras;
        public string Message { get { return message; } set { message = value; OnPropertyChanged(); } }
        private string message;

        public int CurrentDefectType { get { return currentDefectType; } set { currentDefectType = value; OnPropertyChanged(); } }
        private int currentDefectType;

        public ObservableCollection<int> DefectTypeList { get { return defectTypeList; } set { defectTypeList = value; OnPropertyChanged(); } }
        private ObservableCollection<int> defectTypeList;



        public ObservableCollection<object[]> DefectRectList { get { return defectRectList; } set { defectRectList = value; OnPropertyChanged(); } }
        private ObservableCollection<object[]> defectRectList;




        ESDataHelper eshelper;
        int Onlineimagescount = 24;
        DataFrom database = StaticData.DataBase;
        ImagesVirtualFactory imagesFactory1;
        ImagesVirtualFactory imagesFactory2;
        ImagesVirtualFactory imagesFactory3;
        ImagesVirtualFactory imagesFactory4;

        DateTime latestImageInsertTime1 = new DateTime(1900, 1, 1);
        DateTime latestImageInsertTime2 = new DateTime(1900, 1, 1);
        DateTime latestImageInsertTime3 = new DateTime(1900, 1, 1);
        DateTime latestImageInsertTime4 = new DateTime(1900, 1, 1);
        bool IsFirst = true;

        //自动刷新时保留的最大数量
        int ImageListMaxLength = 1;
        public System.Timers.Timer timer;

        #endregion

        public VM_PageImageView()
        {

            InitData();



        }

        bool isLoad = false;
        private void InitData()
        {

            eshelper = new ESDataHelper();
            Cameras = new ObservableCollection<int>() { 1, 2, 3, 4, 5, 6, 7, 8 };
            CurrentCamera = 1;
            CurrentImageType = 1;
            SurfacePartInfoList = new ObservableCollection<SurfacePartInfo>();
            Start = DateTime.Now.AddDays(-10);
            End = DateTime.Now;
            UseStart = true;
            UseEnd = true;
            IsQueryCompleted = true;
            Imagelist1 = new ObservableCollection<Imagedefectinfo>();
            Imagelist2 = new ObservableCollection<Imagedefectinfo>();
            Imagelist3 = new ObservableCollection<Imagedefectinfo>();
            Imagelist4 = new ObservableCollection<Imagedefectinfo>();
            Imagelist5 = new ObservableCollection<Imagedefectinfo>();
            Imagelist6 = new ObservableCollection<Imagedefectinfo>();
            Imagelist7 = new ObservableCollection<Imagedefectinfo>();
            Imagelist8 = new ObservableCollection<Imagedefectinfo>();
            CurrentImg1 = new Imagedefectinfo() { CurrentImage = new image() { id = "", type = 1 } };
            CurrentImg2 = new Imagedefectinfo() { CurrentImage = new image() { id = "", type = 2 } };
            CurrentImg3 = new Imagedefectinfo() { CurrentImage = new image() { id = "", type = 3 } };
            CurrentImg4 = new Imagedefectinfo() { CurrentImage = new image() { id = "", type = 4 } };
            CurrentImg5 = new Imagedefectinfo() { CurrentImage = new image() { id = "", type = 5 } };
            CurrentImg6 = new Imagedefectinfo() { CurrentImage = new image() { id = "", type = 6 } };
            CurrentImg7 = new Imagedefectinfo() { CurrentImage = new image() { id = "", type = 7 } };
            CurrentImg8 = new Imagedefectinfo() { CurrentImage = new image() { id = "", type = 8 } };
            CurrentImgList = new List<Imagedefectinfo>() { CurrentImg1, CurrentImg2, CurrentImg3, CurrentImg4, CurrentImg5, CurrentImg6, CurrentImg7, CurrentImg8 };
            ImgList_Collection = new List<ObservableCollection<Imagedefectinfo>>() { Imagelist1, Imagelist2, Imagelist3, Imagelist4, Imagelist5, Imagelist6, Imagelist7, Imagelist8 };
            DefectRectList = new ObservableCollection<object[]>();

            timer = new System.Timers.Timer();
            timer.Interval = 2000;
            timer.Elapsed += (s, e) =>
            {
                if (StaticData.ActivePage == PageName.PageImageView)
                {
                    if (StaticData.IsRefreshing)
                    {
                        UpdateImageData();
                        IsFirst = false;
                    }
                }
            };
            timer.Start();
            surfaceEntities = new surfacedetectEntities();
            isLoad = true;
            Logger.Info("SuccessInitData");
            Task.Run(() =>
            {
                var batches = new ObservableCollection<batch>(surfaceEntities.batch.AsNoTracking().OrderByDescending(o => o.insert_time).Take(20).ToList());
                DoMenthodByDispatcher(() =>
                {
                    Batchlist = batches;
                    CurrentBatch = Batchlist[0];
                },
                System.Windows.Threading.DispatcherPriority.Background);
                //if (!StaticData.IsRefreshing)
                //    _ = QueryImageInfo();
            });
        }

        CancellationTokenSource cts = new CancellationTokenSource();
        private async Task QueryImageInfo()
        {
            cts = new CancellationTokenSource();
            SurfacePartInfoList.Clear();
            Imagelist1.Clear();
            Imagelist2.Clear();
            Imagelist3.Clear();
            Imagelist4.Clear();
            Imagelist5.Clear();
            Imagelist6.Clear();
            Imagelist7.Clear();
            Imagelist8.Clear();

            var size = 20;
            int flowid = 1;
            IsQueryCompleted = false;
            await Task.Run(async () =>
            {
                while (true)
                {
                    try
                    {
                        if (cts.IsCancellationRequested)
                            break;
                        size = flowid < 20 ? 20 : 100;
                        var imageinfolist = await new DefectDataHelper().getImageInfoList(CurrentBatch.main_id, size, flowid);
                        new DefectDataHelper().setCurrentImageByImageType(imageinfolist, CurrentImageType);
                        imageinfolist.All(imageinfo => { imageinfo.ImageStatus = ImageInfoHelper.GetImageStatus(CurrentBatch.img_status); return true; });

                        if (imageinfolist.Count == 0)
                            break;
                        flowid = flowid + size;

                        foreach (var listbycam in imageinfolist.GroupBy(g => g.Image.camera_id))
                        {
                            switch (listbycam.Key)
                            {

                                case 1:
                                    await Application.Current.Dispatcher.BeginInvoke(new Action(() => { foreach (var item in listbycam) { Imagelist1.Add(item); }; }));
                                    break;
                                case 2:
                                    await Application.Current.Dispatcher.BeginInvoke(new Action(() => { foreach (var item in listbycam) { Imagelist2.Add(item); }; }));
                                    break;
                                case 3:
                                    await Application.Current.Dispatcher.BeginInvoke(new Action(() => { foreach (var item in listbycam) { Imagelist3.Add(item); }; }));
                                    break;
                                case 4:
                                    await Application.Current.Dispatcher.BeginInvoke(new Action(() => { foreach (var item in listbycam) { Imagelist4.Add(item); }; }));
                                    break;
                                case 5:
                                    await Application.Current.Dispatcher.BeginInvoke(new Action(() => { foreach (var item in listbycam) { Imagelist5.Add(item); }; }));
                                    break;
                                case 6:
                                    await Application.Current.Dispatcher.BeginInvoke(new Action(() => { foreach (var item in listbycam) { Imagelist6.Add(item); }; }));
                                    break;
                                case 7:
                                    await Application.Current.Dispatcher.BeginInvoke(new Action(() => { foreach (var item in listbycam) { Imagelist7.Add(item); }; }));
                                    break;
                                case 8:
                                    await Application.Current.Dispatcher.BeginInvoke(new Action(() => { foreach (var item in listbycam) { Imagelist8.Add(item); }; }));
                                    break;
                            }
                        }
                        var groups = imageinfolist.GroupBy(t => t.Image.flow_id);
                        foreach (var group in groups)
                        {
                            SurfacePartInfo spiinfo = new SurfacePartInfo();
                            spiinfo.Flowid = group.Key;
                            var cameraa3Group = group.Where(t => t.Image.camera_id == 3);
                            spiinfo.steel_length = cameraa3Group.Any() ? (double)cameraa3Group.First().Image.steel_length : 0;
                            int s1count = group.Where(t => t.Image.surface_id == 1).SelectMany(d => d.Image_DefectList).Count();
                            int s2count = group.Where(t => t.Image.surface_id == 2).SelectMany(d => d.Image_DefectList).Count();
                            int s3count = group.Where(t => t.Image.surface_id == 3).SelectMany(d => d.Image_DefectList).Count();
                            int s4count = group.Where(t => t.Image.surface_id == 4).SelectMany(d => d.Image_DefectList).Count();
                            int s5count = group.Where(t => t.Image.surface_id == 5).SelectMany(d => d.Image_DefectList).Count();
                            int s6count = group.Where(t => t.Image.surface_id == 6).SelectMany(d => d.Image_DefectList).Count();
                            if (s1count > 0) spiinfo.DefectCountInS1 = s1count;
                            if (s2count > 0) spiinfo.DefectCountInS2 = s2count;
                            if (s3count > 0) spiinfo.DefectCountInS3 = s3count;
                            if (s4count > 0) spiinfo.DefectCountInS4 = s4count;
                            if (s5count > 0) spiinfo.DefectCountInS5 = s5count;
                            if (s6count > 0) spiinfo.DefectCountInS6 = s6count;
                            await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                            {
                                SurfacePartInfoList.Add(spiinfo);
                            }), System.Windows.Threading.DispatcherPriority.Send);

                        }
                        Thread.Sleep(20);
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e);
                        break;
                    }

                }
                //避免各个相机图片数量不一致情况，统一补足为最大数量

                await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    List<ObservableCollection<Imagedefectinfo>> listlist = new List<ObservableCollection<Imagedefectinfo>>() { Imagelist1, Imagelist2, Imagelist3, Imagelist4, Imagelist5, Imagelist6, Imagelist7, Imagelist8 };
                    int maxcount = listlist.Max(t => t.Count);
                    foreach (var list in listlist)
                    {
                        while (list.Count < maxcount)
                        {
                            list.Add(new Imagedefectinfo()
                            {
                                Image = new image()
                                {
                                    image_url = ""
                                }
                            });
                        }
                    }

                }), System.Windows.Threading.DispatcherPriority.Background);
            }, cts.Token);
            IsQueryCompleted = true;
        }

        public void AddImages(ObservableCollection<Imagedefectinfo> currentlist, IList<Imagedefectinfo> addeddata)
        {

            foreach (var item in addeddata)
            {
                currentlist.Add(item);
            }
        }

        public BaseCommand SearchCommand { get => new BaseCommand(SearchCommand_Executed); }
        public void SearchCommand_Executed(object obj)
        {
            QueryImageInfo();
        }
        public BaseCommand CancelQueryCommand
        {
            get => new BaseCommand((obj) =>
            {
                cts.Cancel();
            });
        }

        public BaseCommand SearchBatchCommand { get => new BaseCommand(SearchBatchCommand_Executed); }
        public void SearchBatchCommand_Executed(object obj)
        {
            var query = surfaceEntities.batch.AsNoTracking().AsQueryable();

            if (UseStart)
                query = query.Where(t => t.insert_time >= Start);
            if (UseEnd)
                query = query.Where(t => t.insert_time <= End);


            Batchlist = new ObservableCollection<batch>(query.OrderByDescending(o => o.insert_time).ToList());
            if (Batchlist.Any())
                CurrentBatch = Batchlist[0];

        }
        public BaseCommand SearchSingleBatchCommand
        {
            get => new BaseCommand((obj) =>
            {
                var batchid = obj as string;
                var batches = surfaceEntities.batch.Where(t => t.main_id == batchid || t.user_custom_id == batchid).ToList();
                if (batches.Any())
                    CurrentBatch = batches[0];
                else
                    MessageBox("未找到对应批次，请输入正确的批次号");
            });
        }

        public BaseCommand GoToPreviousCommand
        {
            get => new BaseCommand((obj) =>
            {
                

                
            });
        }

        public BaseCommand GoToNextCommand
        {
            get => new BaseCommand((obj) =>
            {
                var batchid = obj as string;
                var batches = surfaceEntities.batch.Where(t => t.main_id == batchid || t.user_custom_id == batchid).ToList();
                if (batches.Any())
                    CurrentBatch = batches[0];
                else
                    MessageBox("未找到对应批次，请输入正确的批次号");
            });
        }




        public async Task updataLastImage(int cameraid, int imagetype)
        {
            await Task.Run(async () =>
            {

                var datahelper = new DefectDataHelper();
                var image = await datahelper.getLastImage(cameraid, imagetype);
                if (image.CurrentImage==null) 
                    return;

                _ = Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    try { 
                    Imagedefectinfo info = new Imagedefectinfo();
                    switch (cameraid)
                    {
                        case 1: if (CurrentImg1 != null && CurrentImg1.CurrentImage != null && CurrentImg1.CurrentImage.image_url == image.CurrentImage.image_url) return; CurrentImg1 = image; break;
                        case 2: if (CurrentImg2 != null && CurrentImg2.CurrentImage != null && CurrentImg2.CurrentImage.image_url == image.CurrentImage.image_url) return; CurrentImg2 = image; break;
                        case 3: if (CurrentImg3 != null && CurrentImg3.CurrentImage != null && CurrentImg3.CurrentImage.image_url == image.CurrentImage.image_url) return; CurrentImg3 = image; break;
                        case 4: if (CurrentImg4 != null && CurrentImg4.CurrentImage != null && CurrentImg4.CurrentImage.image_url == image.CurrentImage.image_url) return; CurrentImg4 = image; break;
                        case 5: if (CurrentImg5 != null && CurrentImg5.CurrentImage != null && CurrentImg5.CurrentImage.image_url == image.CurrentImage.image_url) return; CurrentImg5 = image; break;
                        case 6: if (CurrentImg6 != null && CurrentImg6.CurrentImage != null && CurrentImg6.CurrentImage.image_url == image.CurrentImage.image_url) return; CurrentImg6 = image; break;
                        case 7: if (CurrentImg7 != null && CurrentImg7.CurrentImage != null && CurrentImg7.CurrentImage.image_url == image.CurrentImage.image_url) return; CurrentImg7 = image; break;
                        case 8: if (CurrentImg8 != null && CurrentImg8.CurrentImage != null && CurrentImg8.CurrentImage.image_url == image.CurrentImage.image_url) return; CurrentImg8 = image; break;
                    }
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e);
                    }


                    //if (info == null || (image.CurrentImage != null || image.CurrentImage.id != info.CurrentImage.id))
                    //{
                    //    info = image;
                    //}
                }), System.Windows.Threading.DispatcherPriority.Background);
            });


        }

        string mainid = "";
        /// <summary>
        /// 更新数据
        /// </summary>
        private void UpdateImageData()
        {
            try
            {

                var datahelper = new DefectDataHelper();
                //Diff 根据相机数循环更新
                //永钢4个相机，兴橙6个相机,马钢7个相机，南南铝8个相机
                for (int i = 1; i <= 7; i++)
                {
                    _ = updataLastImage(i, CurrentImageType);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
            }

        }

        public void updateImage()
        {
            if (!isLoad)
                return; ;
            if (IsRefreshing) {
                UpdateImageData();
            }
            else
            {
                var datahelper = new DefectDataHelper();
                datahelper.setCurrentImageByImageType(Imagelist1, CurrentImageType);
                datahelper.setCurrentImageByImageType(Imagelist2, CurrentImageType);
                datahelper.setCurrentImageByImageType(Imagelist3, CurrentImageType);
                datahelper.setCurrentImageByImageType(Imagelist4, CurrentImageType);
                datahelper.setCurrentImageByImageType(Imagelist5, CurrentImageType);
                datahelper.setCurrentImageByImageType(Imagelist6, CurrentImageType);
                datahelper.setCurrentImageByImageType(Imagelist7, CurrentImageType);
                datahelper.setCurrentImageByImageType(Imagelist8, CurrentImageType);
                var temp1 = CurrentImg1; CurrentImg1 = null; CurrentImg1 = temp1;
                var temp2 = CurrentImg2; CurrentImg2 = null; CurrentImg2 = temp2;
                var temp3 = CurrentImg3; CurrentImg3 = null; CurrentImg3 = temp3;
                var temp4 = CurrentImg4; CurrentImg4 = null; CurrentImg4 = temp4;
                var temp5 = CurrentImg5; CurrentImg5 = null; CurrentImg5 = temp5;
                var temp6 = CurrentImg6; CurrentImg6 = null; CurrentImg6 = temp6;
                var temp7 = CurrentImg7; CurrentImg7 = null; CurrentImg7 = temp7;
                var temp8 = CurrentImg8; CurrentImg8 = null; CurrentImg8 = temp8;
            }

            
        }

        private BitmapImage getImage(string url)
        {
            if (string.IsNullOrEmpty(url))
                return null;
            BitmapImage bit = new BitmapImage();

            bit.BeginInit();
            bit.UriSource = new Uri(url);
            bit.EndInit();
            bit.Freeze();
            return bit;
        }


        private void RemoveList(ObservableCollection<Imagedefectinfo> Imagedefectinfos)
        {
            var count = Imagedefectinfos.Count;
            for (int i = 0; i < count - ImageListMaxLength; i++)
            {
                Imagedefectinfos.Remove(Imagedefectinfos.Last());
            }

        }

        public void StartRefreshing()
        {
            QueryCtlVisiable = Visibility.Hidden;
        }
        public void StopRefreshing()
        {
            QueryCtlVisiable = Visibility.Visible;
            if (CurrentBatch != null && !Imagelist1.Any())
            {
                _ = QueryImageInfo();
            }
            RequestUpdateCurrentHistoryImage?.Invoke(this, null);
            //updateImage();
        }
        //给PageImageView调用，用来在停止刷新时更新当前显示的历史图像
        public event EventHandler RequestUpdateCurrentHistoryImage;

    }

    public class ImagesByCamera : BaseViewModel
    {
        public Imagedefectinfo Image1 { get { return image1; } set { image1 = value; OnPropertyChanged(); } }
        private Imagedefectinfo image1;

        public Imagedefectinfo Image2 { get { return image2; } set { image2 = value; OnPropertyChanged(); } }
        private Imagedefectinfo image2;

        public Imagedefectinfo Image3 { get { return image3; } set { image3 = value; OnPropertyChanged(); } }
        private Imagedefectinfo image3;

        public Imagedefectinfo Image4 { get { return image4; } set { image4 = value; OnPropertyChanged(); } }
        private Imagedefectinfo image4;
    }

    public class PanoData : BaseViewModel
    {
        Rect imageRect;
        BitmapImage image;
        List<Rect> defectRects;

        public Rect ImageRect { get => imageRect; set { imageRect = value; OnPropertyChanged(); } }
        public BitmapImage Image { get => image; set { image = value; OnPropertyChanged(); } }
        public List<Rect> DefectRects { get => defectRects; set { defectRects = value; OnPropertyChanged(); } }
    }

    public class SurfacePartInfo : INotifyPropertyChanged
    {
        public int Flowid { get; set; }
        public double steel_length { get; set; }
        public Nullable<int> DefectCountInS1 { get; set; }
        public Nullable<int> DefectCountInS2 { get; set; }
        public Nullable<int> DefectCountInS3 { get; set; }
        public Nullable<int> DefectCountInS4 { get; set; }
        public Nullable<int> DefectCountInS5 { get; set; }
        public Nullable<int> DefectCountInS6 { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

    }


}
